
package com.ruoyi.controller;

import com.ruoyi.pojo.Vehicle;
import com.ruoyi.pojo.VehicleModel;
import com.ruoyi.service.VehicleModelService;
import com.ruoyi.service.VehicleService;
import com.ruoyi.utils.PinyinFuzzySearchUtil;
import com.ruoyi.vo.VehicleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/vehicle")
public class VehicleController {
    @Autowired
    private VehicleModelService vehicleModelService;

    @Autowired
    private VehicleService vehicleService;

    /**
     * 车辆和类型的两表联查,并且根据vin名称字段模糊查询
     * @return
     */
    @GetMapping("/selectVehicle")
    public List<String> getVehicle(String vin) {
        //现查到所有的数据，把数据的名称加入到工具类的方法里面
        List<Vehicle> vehicleModels = vehicleService.selectAll();
        List<String> vehicleList = new ArrayList<>();
        for (Vehicle vehicleModel : vehicleModels) {
            vehicleList.add(vehicleModel.getVin());
        }
        List<String> car = PinyinFuzzySearchUtil.fuzzyQuery(vin, vehicleList);
        System.out.println(car);
        //循环把根据pinyin插件查询到的名称调用条件查询
        List<String> strings = new ArrayList<>();
        for (String carVin : car) {
            List<VehicleVo> list = vehicleService.selectByVehicleAnoModel(carVin);
            strings.add(list.toString());
        }
        return strings;

    }

    /**
     * 查询车辆的接口
     *
     * @return
     */
    @GetMapping("/selectAllVehicle")
    public List<Vehicle> selectAllVehicle() {
        List<Vehicle> list = vehicleService.selectAll();
        return list;
    }

    /**
     * 查询车辆类型的接口
     *
     * @return
     *
     */
    @GetMapping("/selectAllVehicleModel")
    public List<VehicleModel> selectAllVehicleModel() {
        return vehicleModelService.selectAllVehicleModel();
    }

    /**
     * 车辆添加
     *
     * @param vehicle
     * @return
     */
    @PostMapping("/insertVehicle")
    public String insertVehicle(@RequestBody Vehicle vehicle) {
        boolean code = vehicleService.insertVehicle(vehicle);
        if (code) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 添加车辆类型
     *
     * @param vehicleModel
     * @return
     */
    @PostMapping("/insertVehicleModel")
    public String insertVehicleModel(@RequestBody VehicleModel vehicleModel) {
        boolean code = vehicleModelService.insertVehicleModel(vehicleModel);
        if (code) {
            return "添加成功";
        }
        return "添加失败";
    }

    /**
     * 汽车类型表删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteVehicleModelById")
    public String deleteVehicleModelById(@RequestParam("id") Long id) {
        boolean code = vehicleModelService.deleteVehicleModel(id);
        if (code) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 删除汽车
     *
     * @param id
     * @return
     */
    @DeleteMapping("/deleteVehicleById")
    public String deleteVehicleById(@RequestParam("id") Long id) {
        boolean code = vehicleService.deleteById(id);
        if (code) {
            return "删除成功";
        }
        return "删除失败";
    }

    /**
     * 修改汽车表
     *
     * @param vehicle
     * @return
     */
    @PostMapping("/updateVehicle")
    public String updateVehicle(@RequestBody Vehicle vehicle) {
        boolean code = vehicleService.updateVehicle(vehicle);
        if (code) {
            return "修改成功";
        }
        return "修改失败";
    }


    /**
     * 修改汽车类型表
     *
     * @param vehicleModel
     * @return
     */
    @PostMapping("/updateVehicleModel")
    public String updateVehicleModel(@RequestBody VehicleModel vehicleModel) {
        boolean code = vehicleModelService.updateVehicleModel(vehicleModel);
        if (code) {
            return "修改成功";
        }
        return "修改失败";
    }


}
